# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=ruby

pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.1.2
pkgrel=1
pkgdesc="An object-oriented language for quick and easy programming (mingw-w64)"
arch=('any')
url="http://www.ruby-lang.org/en"
license=("BSD, custom")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc" "${MINGW_PACKAGE_PREFIX}-pkg-config")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
        "${MINGW_PACKAGE_PREFIX}-gdbm"
        "${MINGW_PACKAGE_PREFIX}-libyaml"
        "${MINGW_PACKAGE_PREFIX}-openssl"
        "${MINGW_PACKAGE_PREFIX}-tk"
        "${MINGW_PACKAGE_PREFIX}-ncurses"
        "${MINGW_PACKAGE_PREFIX}-libffi")
options=('staticlibs' 'strip')
source=("http://cache.ruby-lang.org/pub/ruby/${pkgver%.*}/ruby-${pkgver}.tar.bz2"
        0002-use-gnu-printf.patch)
md5sums=('ed9b8565bdeccb401d628ec8d54a0774'
         '6d54f98b9e3a0d0077e9348dc7f69482')

prepare() {
  cd ${srcdir}/ruby-${pkgver}
  patch -p1 -i ${srcdir}/0002-use-gnu-printf.patch
  
  autoreconf -fi
}

build() {

  local FFI_INC=$(pkg-config --cflags libffi)
  CPPFLAGS+=" -DFD_SETSIZE=2048 ${FFI_INC} -I${MINGW_PREFIX}/include/ncurses -I${MINGW_PREFIX}/${MINGW_CHOST}/include"
  CFLAGS+=" -I${MINGW_PREFIX}/include/ncurses ${FFI_INC} -I${MINGW_PREFIX}/${MINGW_CHOST}/include"
  CXXFLAGS+=" -I${MINGW_PREFIX}/include/ncurses ${FFI_INC} -I${MINGW_PREFIX}/${MINGW_CHOST}/include"
  LDFLAGS+=" -L${MINGW_PREFIX}/lib"

  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${srcdir}/ruby-${pkgver}/configure \
    --includedir=${MINGW_PREFIX}/${MINGW_CHOST}/include \
    --disable-install-doc \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST}
  make 
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make - GNUMakefile DESTDIR="${pkgdir}" install-nodoc
  for script in {erb,gem,irb,rake,rdoc,ri,testrb}; do
    install ${srcdir}/ruby-${pkgver}/bin/${script} \
      ${pkgdir}${MINGW_PREFIX}/bin/
    sed -i 's:!/usr/bin/env ruby:!/bin/env ruby:' \
      ${pkgdir}${MINGW_PREFIX}/bin/${script}
  done
  #rm -r "${pkgdir}${MINGW_PREFIX}/share"
}
